package GUI;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import ctrLayer.ForestOwnerCtr;
import ctrLayer.TreeCtr;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * MainFrame with main method and testdata
 * @author Thomas
 *
 */
public class MainFrame extends JFrame {

	private ForestOwnerCtr forestOwnerCtr;
	private TreeCtr treeCtr;
	private JPanel cardLayout;
	
	private JTabbedPane tabbedPanel;
	
	private ForestPanel forestPanel;
	private OwnerPanel ownerPanel;
	private TreePanel treePanel;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
	}

	public MainFrame() {
		forestOwnerCtr = ForestOwnerCtr.getInstance();
		treeCtr = TreeCtr.getInstance();
		initializeFrame();
		initializeCardLayout();
	}
	
	private void initializeFrame() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Forest Assignment");
		setBounds((int) d.getWidth() / 4, (int) d.getHeight() / 5, 800, 600);
		setResizable(false);
		setVisible(true);
	}
	
	private void initializeCardLayout() {
		
		cardLayout = new JPanel();
		cardLayout.setBorder(new EmptyBorder(5,5,5,5));
		setContentPane(cardLayout);
		cardLayout.setLayout(new CardLayout(0, 0));
		
		initializeMenu();
		initializeTabbedPanel();
	}
	
	private void initializeMenu() {
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmInsertTestdata = new JMenuItem("Insert TestData");
		mntmInsertTestdata.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				insertTestData();
			}
		});
		mnFile.add(mntmInsertTestdata);
		}
	
	private void initializeTabbedPanel() {
		tabbedPanel = new JTabbedPane(JTabbedPane.TOP);
		cardLayout.add(tabbedPanel, "tabbedPanel");
		
		forestPanel = ForestPanel.getInstance();
		ownerPanel = OwnerPanel.getInstance();
		treePanel = TreePanel.getInstance();
		
		tabbedPanel.addTab("Forest", null, forestPanel, null);
		tabbedPanel.addTab("Owner", null, ownerPanel, null);
		tabbedPanel.addTab("Tree", null, treePanel, null);
	}
	
	private void insertTestData() {
		
		forestOwnerCtr.createOwner("Hans", "Roldvej 10", "12345678");
		forestOwnerCtr.createOwner("Ruben", "Aalborgvej 15", "23456789");
		treeCtr.createTree("Ash", 100, 50, 10);
		treeCtr.createTree("Birch", 200, 25, 15);
		forestOwnerCtr.CreateForest("RoldSkov", "Rold", 80, "Temperate forests ", 10, 1, 1);
		forestOwnerCtr.CreateForest("AalborgSkov", "Aalborg", 40, "Mediterranean forests", 20, 1, 2);
		forestOwnerCtr.CreateForest("RebildSkov", "Rebild", 120, "Temperate forests ", 35, 2, 2);
		forestOwnerCtr.CreateForest("SkagenSkov", "Skagen", 20, "Mediterranean forests", 5, 2, 2);
		forestPanel.updateTable();
		ownerPanel.updatePanel();
		treePanel.updatePanel(treeCtr.treeListToArray());
		
	}

}
